<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java集合框架线程安全解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=NotoSans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", 
                        "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .hero {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .title {
            font-family: 'Noto Serif SC', serif;
            font-weight: 700;
        }
        .card {
            transition: all 0.3s ease;
            box-shadow: 0 10px 20px rgba(0,0,0,0.1);
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 30px rgba(0,0,0,0.15);
        }
        .section-divider {
            height: 2px;
            background: linear-gradient(90deg, transparent, rgba(0,0,0,0.1), transparent);
            margin: 3rem 0;
        }
        .step-icon {
            width: 40px;
            height: 40px;
            background: #4f46e5;
            color: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 1rem;
            flex-shrink: 0;
        }
        .code-block {
            background: #f8f9fa;
            border-left: 4px solid #4f46e5;
            padding: 1rem;
            border-radius: 0 4px 4px 0;
            font-family: monospace;
            overflow-x: auto;
        }
        .highlight {
            color: #4f46e5;
            font-weight: 600;
        }
        .solution-card {
            border-top: 4px solid #10b981;
        }
        .footer {
            background: #1a1a1a;
        }
        a:hover {
            color: #818cf8;
        }
        .mermaid {
            background: white;
            padding: 1rem;
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4">
        <div class="container mx-auto max-w-4xl">
            <h1 class="title text-4xl md:text-5xl mb-4">Java集合框架线程安全解析</h1>
            <p class="text-xl opacity-90 mb-8">深入理解HashMap、ArrayList的线程安全问题与解决方案</p>
            <div class="flex items-center">
                <div class="w-12 h-12 rounded-full bg-white bg-opacity-20 flex items-center justify-center mr-4">
                    <i class="fas fa-lock-open text-xl"></i>
                </div>
                <p class="opacity-90">多线程环境下的集合框架使用指南</p>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-4xl px-4 py-12">
        <!-- Introduction -->
        <section class="mb-16">
            <div class="flex items-start mb-6">
                <div class="mr-4 text-indigo-600">
                    <i class="fas fa-info-circle text-2xl"></i>
                </div>
                <div>
                    <h2 class="title text-2xl font-bold mb-2">线程安全的重要性</h2>
                    <p class="text-gray-600">在多线程编程中，理解集合类的线程安全特性至关重要。不当的使用可能导致数据不一致、程序崩溃等严重问题。</p>
                </div>
            </div>
            
            <div class="grid md:grid-cols-2 gap-6 mt-8">
                <div class="card bg-white p-6 rounded-lg">
                    <div class="flex items-center mb-4">
                        <div class="step-icon">
                            <i class="fas fa-exclamation-triangle"></i>
                        </div>
                        <h3 class="text-xl font-semibold">问题集合</h3>
                    </div>
                    <ul class="space-y-2 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-chevron-right text-indigo-500 mt-1 mr-2"></i>
                            <span>HashMap在多线程环境下的问题</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-chevron-right text-indigo-500 mt-1 mr-2"></i>
                            <span>ArrayList的线程不安全本质</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-chevron-right text-indigo-500 mt-1 mr-2"></i>
                            <span>常见并发修改异常场景</span>
                        </li>
                    </ul>
                </div>
                
                <div class="card bg-white p-6 rounded-lg">
                    <div class="flex items-center mb-4">
                        <div class="step-icon bg-emerald-500">
                            <i class="fas fa-check-circle"></i>
                        </div>
                        <h3 class="text-xl font-semibold">解决方案</h3>
                    </div>
                    <ul class="space-y-2 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-chevron-right text-emerald-500 mt-1 mr-2"></i>
                            <span>ConcurrentHashMap替代方案</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-chevron-right text-emerald-500 mt-1 mr-2"></i>
                            <span>CopyOnWriteArrayList使用场景</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-chevron-right text-emerald-500 mt-1 mr-2"></i>
                            <span>同步控制的最佳实践</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>

        <div class="section-divider"></div>

        <!-- HashMap Section -->
        <section class="mb-16">
            <div class="flex items-start mb-6">
                <div class="mr-4 text-red-500">
                    <i class="fas fa-exclamation text-2xl"></i>
                </div>
                <div>
                    <h2 class="title text-2xl font-bold mb-2">HashMap的线程不安全问题</h2>
                    <p class="text-gray-600">HashMap不是线程安全的容器，在多线程环境下可能导致数据不一致甚至死循环。</p>
                </div>
            </div>

            <div class="grid md:grid-cols-2 gap-8">
                <div>
                    <div class="card bg-white p-6 rounded-lg mb-6">
                        <h3 class="text-lg font-semibold mb-4 flex items-center">
                            <i class="fas fa-bug text-red-500 mr-2"></i>
                            典型问题
                        </h3>
                        <ul class="space-y-3 text-gray-700">
                            <li class="flex">
                                <span class="bg-red-100 text-red-800 px-2 py-1 rounded mr-2">!</span>
                                <span>多线程put操作可能导致数据丢失</span>
                            </li>
                            <li class="flex">
                                <span class="bg-red-100 text-red-800 px-2 py-1 rounded mr-2">!</span>
                                <span>并发扩容可能导致死循环问题</span>
                            </li>
                            <li class="flex">
                                <span class="bg-red-100 text-red-800 px-2 py-1 rounded mr-2">!</span>
                                <span>非原子性操作导致size不一致</span>
                            </li>
                        </ul>
                    </div>

                    <div class="card solution-card bg-white p-6 rounded-lg">
                        <h3 class="text-lg font-semibold mb-4 flex items-center">
                            <i class="fas fa-lightbulb text-emerald-500 mr-2"></i>
                            解决方案
                        </h3>
                        <div class="space-y-4">
                            <div>
                                <h4 class="font-medium mb-2">1. Hashtable</h4>
                                <p class="text-gray-600 text-sm">线程安全但性能较差，使用全表锁</p>
                                <div class="code-block mt-2">
                                    Map&lt;String, String&gt; map = new Hashtable&lt;&gt;();
                                </div>
                            </div>
                            <div>
                                <h4 class="font-medium mb-2">2. ConcurrentHashMap</h4>
                                <p class="text-gray-600 text-sm">Java推荐方案，分段锁提高并发性能</p>
                                <div class="code-block mt-2">
                                    Map&lt;String, String&gt; map = new ConcurrentHashMap&lt;&gt;();
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div>
                    <div class="mermaid">
                        graph TD
                            A[HashMap线程不安全] --> B[数据丢失]
                            A --> C[死循环]
                            A --> D[数据不一致]
                            B --> E[多线程put覆盖]
                            C --> F[并发扩容导致的循环链表]
                            D --> G[非原子操作]
                            H[解决方案] --> I[Hashtable]
                            H --> J[ConcurrentHashMap]
                            I --> K[全表锁性能低]
                            J --> L[分段锁高性能]
                    </div>
                </div>
            </div>
        </section>

        <div class="section-divider"></div>

        <!-- ArrayList Section -->
        <section class="mb-16">
            <div class="flex items-start mb-6">
                <div class="mr-4 text-orange-500">
                    <i class="fas fa-question-circle text-2xl"></i>
                </div>
                <div>
                    <h2 class="title text-2xl font-bold mb-2">ArrayList的线程不安全本质</h2>
                    <p class="text-gray-600">为什么ArrayList在多线程环境下会出现问题？关键在于理解i++的非原子性操作。</p>
                </div>
            </div>

            <div class="grid md:grid-cols-2 gap-8">
                <div>
                    <div class="card bg-white p-6 rounded-lg mb-6">
                        <h3 class="text-lg font-semibold mb-4"><i class="fas fa-code-branch text-orange-500 mr-2"></i> i++操作分解</h3>
                        <p class="text-gray-600 mb-4">i++看似简单，实际上包含三个步骤：</p>
                        <div class="space-y-3">
                            <div class="flex items-start">
                                <div class="bg-orange-100 text-orange-800 px-3 py-1 rounded-full mr-3">1</div>
                                <div>
                                    <div class="font-medium">获取当前值</div>
                                    <div class="text-sm text-gray-500">读取变量i的当前值</div>
                                </div>
                            </div>
                            <div class="flex items-start">
                                <div class="bg-orange-100 text-orange-800 px-3 py-1 rounded-full mr-3">2</div>
                                <div>
                                    <div class="font-medium">计算+1</div>
                                    <div class="text-sm text-gray-500">对读取的值进行+1操作</div>
                                </div>
                            </div>
                            <div class="flex items-start">
                                <div class="bg-orange-100 text-orange-800 px-3 py-1 rounded-full mr-3">3</div>
                                <div>
                                    <div class="font-medium">赋值</div>
                                    <div class="text-sm text-gray-500">将新值写回变量i</div>
                                </div>
                            </div>
                        </div>
                        <p class="mt-4 text-gray-600">这三个步骤在执行过程中可能被其他线程打断，导致数据不一致。</p>
                    </div>

                    <div class="card bg-white p-6 rounded-lg">
                        <img src="https://cdn.nlark.com/yuque/0/2021/png/21449790/1637805286004-9ebb1190-6ab1-4b71-b961-b46a1c575c61.png" alt="ArrayList线程不安全图示" class="rounded-lg shadow-sm mb-4">
                        <p class="text-sm text-gray-500">ArrayList在多线程环境下的并发修改问题示意图</p>
                    </div>
                </div>

                <div>
                    <div class="card solution-card bg-white p-6 rounded-lg h-full">
                        <h3 class="text-lg font-semibold mb-4 flex items-center">
                            <i class="fas fa-shield-alt text-emerald-500 mr-2"></i>
                            ArrayList线程安全解决方案
                        </h3>
                        <div class="space-y-4">
                            <div>
                                <h4 class="font-medium mb-2">CopyOnWriteArrayList</h4>
                                <p class="text-gray-600 text-sm">读操作无锁，写操作通过复制数组实现线程安全</p>
                                <div class="code-block mt-2">
                                    List&lt;String&gt; list = new CopyOnWriteArrayList&lt;&gt;();
                                </div>
                                <p class="mt-2 text-sm text-gray-600">适用读多写少的场景，写操作性能较差但读操作性能极高。</p>
                            </div>
                            <div class="mt-6">
                                <h4 class="font-medium mb-2">Collections.synchronizedList</h4>
                                <p class="text-gray-600 text-sm">通过同步包装器实现线程安全</p>
                                <div class="code-block mt-2">
                                    List&lt;String&gt; list = Collections.synchronizedList(new ArrayList&lt;&gt;());
                                </div>
                                <p class="mt-2 text-sm text-gray-600">读写操作都加锁，性能较差但实现简单。</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Comparison Section -->
        <section class="mb-16">
            <h2 class="title text-2xl font-bold mb-8 text-center">线程安全集合对比</h2>
            <div class="overflow-x-auto">
                <table class="min-w-full bg-white rounded-lg overflow-hidden shadow">
                    <thead class="bg-indigo-600 text-white">
                        <tr>
                            <th class="py-3 px-4 text-left">集合类型</th>
                            <th class="py-3 px-4 text-left">线程安全实现方式</th>
                            <th class="py-3 px-4 text-left">性能特点</th>
                            <th class="py-3 px-4 text-left">适用场景</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        <tr>
                            <td class="py-3 px-4 font-medium">HashMap</td>
                            <td class="py-3 px-4 text-red-500">非线程安全</td>
                            <td class="py-3 px-4">高性能</td>
                            <td class="py-3 px-4">单线程环境</td>
                        </tr>
                        <tr class="bg-gray-50">
                            <td class="py-3 px-4 font-medium">Hashtable</td>
                            <td class="py-3 px-4">全表锁</td>
                            <td class="py-3 px-4">性能较差</td>
                            <td class="py-3 px-4">不推荐使用</td>
                        </tr>
                        <tr>
                            <td class="py-3 px-4 font-medium">ConcurrentHashMap</td>
                            <td class="py-3 px-4">分段锁/CAS</td>
                            <td class="py-3 px-4">高并发性能</td>
                            <td class="py-3 px-4">多线程环境首选</td>
                        </tr>
                        <tr class="bg-gray-50">
                            <td class="py-3 px-4 font-medium">ArrayList</td>
                            <td class="py-3 px-4 text-red-500">非线程安全</td>
                            <td class="py-3 px-4">高性能</td>
                            <td class="py-3 px-4">单线程环境</td>
                        </tr>
                        <tr>
                            <td class="py-3 px-4 font-medium">CopyOnWriteArrayList</td>
                            <td class="py-3 px-4">写时复制</td>
                            <td class="py-3 px-4">读高性能，写性能差</td>
                            <td class="py-3 px-4">读多写少场景</td>
                        </tr>
                        <tr class="bg-gray-50">
                            <td class="py-3 px-4 font-medium">Vector</td>
                            <td class="py-3 px-4">方法级同步</td>
                            <td class="py-3 px-4">性能较差</td>
                            <td class="py-3 px-4">不推荐使用</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </section>

        <!-- Best Practices -->
        <section class="mb-16">
            <div class="card bg-white p-8 rounded-lg">
                <h2 class="title text-2xl font-bold mb-6 text-center">多线程集合使用最佳实践</h2>
                <div class="grid md:grid-cols-3 gap-6">
                    <div class="text-center">
                        <div class="w-16 h-16 bg-indigo-100 rounded-full flex items-center justify-center mx-auto mb-4">
                            <i class="fas fa-search text-indigo-600 text-2xl"></i>
                        </div>
                        <h3 class="font-semibold mb-2">明确需求</h3>
                        <p class="text-gray-600 text-sm">根据读写比例和性能要求选择合适的线程安全集合</p>
                    </div>
                    <div class="text-center">
                        <div class="w-16 h-16 bg-indigo-100 rounded-full flex items-center justify-center mx-auto mb-4">
                            <i class="fas fa-tachometer-alt text-indigo-600 text-2xl"></i>
                        </div>
                        <h3 class="font-semibold mb-2">性能优先</h3>
                        <p class="text-gray-600 text-sm">优先考虑ConcurrentHashMap和CopyOnWriteArrayList</p>
                    </div>
                    <div class="text-center">
                        <div class="w-16 h-16 bg-indigo-100 rounded-full flex items-center justify-center mx-auto mb-4">
                            <i class="fas fa-ban text-indigo-600 text-2xl"></i>
                        </div>
                        <h3 class="font-semibold mb-2">避免过度同步</h3>
                        <p class="text-gray-600 text-sm">不要使用过时的同步集合如Vector和Hashtable</p>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="footer text-white py-8">
        <div class="container mx-auto max-w-4xl px-4">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <h3 class="text-lg font-medium">技术小馆</h3>
                    <p class="text-gray-400">专业的技术知识分享平台</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="hover:text-indigo-300 transition-colors">
                        <i class="fas fa-globe mr-2"></i>技术小馆地址：http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>